package com.txsj.scan.test;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.nativec.tools.ModuleManager;
import com.nativec.tools.SerialPort;
import com.nativec.tools.SerialPortFinder;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.MyApplication;
import com.txsj.scan.R;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.reader.helper.ReaderHelper;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

/**
 * 临时读取标签
 */

public class ReadSignActivity extends BaseActivity {

    //add by lei.li 2016//11/14
    private static final String TAG = "COONECTRS232";
    private static final String TTYS1 = "ttyS4 (rk_serial)";   //串口号
    private int baud = 115200;      //波特率
    private static final boolean DEBUG = true;
    //add by lei.li 2016//11/14
    private TextView mConectButton;
    private ReaderHelper mReaderHelper;                         //
    private List<String> mPortList = new ArrayList<String>();   //查到串口后的集合
    private int mPosPort = -1;                          //当前选择的串口
    private SerialPortFinder mSerialPortFinder;     //串口查找器

    String[] entries = null;                    //串口查到所有设备后返回的数组
    String[] entryValues = null;                //串口查到后设备对应的路径

    private TextView mPortTextView, mBaud115200View;

    public static SerialPort mSerialPort = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_sign);
        MyApplication.getInstance().addActivity(this);

        mSerialPortFinder = new SerialPortFinder();
        entries = mSerialPortFinder.getAllDevices();
        entryValues = mSerialPortFinder.getAllDevicesPath();

        mConectButton = (TextView) findViewById(R.id.textview_connect);
        mPortTextView =  (TextView) findViewById(R.id.comport_text);
        mBaud115200View =  (TextView) findViewById(R.id.baud_115200);
        mPortTextView.setText(TTYS1);
        mBaud115200View.setText(baud+"");

        String[] lists = entries;
        for(int i = 0; i < lists.length; i++){
            mPortList.add(lists[i]);
        }



        mConectButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mPortList.indexOf(mPortTextView.getText().toString()) >= 0)
                    mPosPort = mPortList.indexOf(mPortTextView.getText().toString());

                if (DEBUG)
                    Log.e(TAG, "test the value of mPosPort::" + mPosPort);

                if (mPosPort < 0) {
                    Toast.makeText(
                            getApplicationContext(),
                            getResources().getString(R.string.rs232_error),
                            Toast.LENGTH_SHORT).show();
                    return ;
                }

                try {

                    mSerialPort = new SerialPort(new File(entryValues[mPosPort]), baud, 0);

                    if (DEBUG)
                        Log.e(TAG,"ttys1 value :::" + entryValues[mPosPort]);

                    try {
                        mReaderHelper = ReaderHelper.getDefaultHelper();
                        mReaderHelper.setReader(mSerialPort.getInputStream(), mSerialPort.getOutputStream());
                    } catch (Exception e) {
                        e.printStackTrace();

                        return ;
                    }
                    if (!ModuleManager.newInstance().setUHFStatus(true)) {
                        throw new RuntimeException("UHF RFID power on failure,may you open in other" +
                                " Process and do not exit it");
                    }

                    Intent intent;
                    intent = new Intent().setClass(ReadSignActivity.this, ReadScanActivity.class);
                    startActivity(intent);
                    //finish();
                } catch (SecurityException e) {
                    Toast.makeText(
                            getApplicationContext(),
                            getResources().getString(R.string.error_security),
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(
                            getApplicationContext(),
                            getResources().getString(R.string.error_unknown),
                            Toast.LENGTH_SHORT).show();
                } catch (InvalidParameterException e) {
                    Toast.makeText(
                            getApplicationContext(),
                            getResources().getString(R.string.error_configuration),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    @Override
    protected BasePresenter providePresenter() {
        return null;
    }
}
